package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {



    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private IStrategyService strategyService;


    @Override
    public void replynumIncrease(String sid, int i) {

        StrategyStatisVO vo = this.getStrategyStatisVo(sid);

        vo.setReplynum(vo.getReplynum() + i);

        this.setStrategyStatisVo(vo);
    }

    @Override
    public void viewnumIncrease(String sid, int i) {

        StrategyStatisVO vo = this.getStrategyStatisVo(sid);

        vo.setViewnum(vo.getViewnum() + i);

        this.setStrategyStatisVo(vo);
    }


    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }


    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO vo = null;
        if (!template.hasKey(key)) {//判断可以是否存在  存在就添加 不存在就新建

            Strategy strategy = strategyService.findById(sid);
            vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy,vo);


            vo.setStrategyId(sid);


            template.opsForValue().set(key, JSON.toJSONString(vo));
        }else{

            String voStr = template.opsForValue().get(key);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        }

        return vo;
    }


    @Override
    public boolean favor(String sid, String uid) {

        String listKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        List<String> list = null;
        if(template.hasKey(listKey)){ //如果已经存在了，获取
            String listStr = template.opsForValue().get(listKey);

            list = JSON.parseArray(listStr, String.class);
        }else{

            list = new ArrayList<>();
        }

        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        boolean flag = true;
        if(!list.contains(sid)){

            vo.setFavornum(vo.getFavornum() + 1);
            list.add(sid);
            flag  = true;
        }else{

            vo.setFavornum(vo.getFavornum() - 1);
            list.remove(sid);
            flag  = false;
        }
        //5:更新list, vo
        template.opsForValue().set(listKey, JSON.toJSONString(list));
        this.setStrategyStatisVo(vo);

        return flag/*list.contains(sid)*/;  //只要list中存在攻略id，表示收藏操作
    }


    @Override
    public List<String> getSids(String uid) {
        String listKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        List<String> list = null;  //攻略id收藏集合list
        if(template.hasKey(listKey)){ //如果已经存在了，获取
            String listStr = template.opsForValue().get(listKey);

            list = JSON.parseArray(listStr, String.class);
        }else{

            list = new ArrayList<>();
        }
        return list;
    }

    @Override
    public boolean thumbup(String sid, String uid) {

        String signKey = RedisKeys.STRATEGY_THUMBUP.join(uid, sid);

        if(!template.hasKey(signKey)){


            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            this.setStrategyStatisVo(vo);
            Date now = new Date();

            Date endDate = DateUtil.getEndDate(now);  //今天最后一秒时间对象
            long time = DateUtil.getDateBetween(now, endDate);  //有效时间， 单位秒
            template.opsForValue().set(signKey, "1",time,TimeUnit.SECONDS);
            return true;
        }


        return false;
    }

    @Override
    public boolean isVoExsits(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> listStrategyVoByPattern(String pattern) {


        Set<String> keys = template.keys(pattern);

        List<StrategyStatisVO> list = new ArrayList<>();
        if(keys != null && keys.size() > 0){
            for (String voKey : keys) {
                String voStr = template.opsForValue().get(voKey);
                list.add(JSON.parseObject(voStr, StrategyStatisVO.class));
            }
        }
        return list;
    }
}
